<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>菜品类型表</title>
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../static/layuiadmin/layui/css/layui.css"
          media="all"/>
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../static/layuiadmin/layui/css/layui.css"
          media="all"/>
    <style>
        body{
            margin: 10px;
            background: #eeeeee;
        }
    </style>
</head>
<body>
    <div class="layui-card">
        <div class="layui-card-body">
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>
                    </button>
                    <button class="layui-btn layui-btn-sm" lay-event="update"><i class="layui-icon layui-icon-edit"></i>
                    </button>
                    <button class="layui-btn layui-btn-sm" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>
                    </button>
                </div>
            </script>
            <!-- table表格-->
            <table id="tableList" lay-filter="tableList"></table>
            <!--右边工具栏-->
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
    <script th:src="@{/layuiadmin/layui/layui.js}" src="../static/layuiadmin/layui/layui.js"></script>
    <script th:inline="javascript">
        layui.use(['table','laydate'], function () {
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table
                ,laydate = layui.laydate;

            //执行一个 table 实例
            table.render({
                elem: '#tableList'
                , height: 620
                , url: "/restaurant/goodscategory/list.do" //数据接口
                , method: 'post'
                , title: '菜品类型表'
                , response: {
                    statusCode: 200
                }
                //,size:"lg"设置表格尺寸
                , toolbar: '#test-table-toolbar-toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'categoryId', title: 'ID',align:'center', cellMinWidth: 80, sort: true, fixed: 'left', totalRowText: '合计：'}
                    , {field: 'categoryName', title: '类型名',align:'center', cellMinWidth: 150}
                    , {fixed: 'right', title: "工具栏",align:'center', width: 180, toolbar: '#barDemo'}
                ]]
            });

            //监听头工具栏事件
            table.on('toolbar(tableList)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id)
                    , data = checkStatus.data; //获取选中的数据
                switch (obj.event) {
                    case 'add':
                        layer.open({
                            type: 2
                            ,title: '添加菜品类型'
                            ,content: '/restaurant/goodscategory/add.html'
                            ,maxmin: true
                            ,area: ['350px', '200px']
                            ,btn: ['确定', '取消']
                            ,yes: function(index, layero){
                                var iframeWindow = window['layui-layer-iframe'+ index]
                                    //该id是窗口打开html页面中表单提交按钮的id
                                    ,submitID = 'LAY-user-front-submit'
                                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                //监听提交
                                iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                    var field = data.field; //获取提交的字段
                                    //提交 Ajax 成功后，静态更新表格中的数据
                                    $.ajax({
                                        url: "/restaurant/goodscategory/add.do",
                                        type: "post",
                                        dataType: "JSON",
                                        data: data.field,
                                        success: function(result) {
                                            if(result.code == 200) {
                                                table.reload('tableList'); //数据刷新
                                                layer.close(index); //关闭弹层
                                            }else {
                                                layer.alert(result.msg, {
                                                    title: '提交结果'
                                                    ,anim: 6
                                                });
                                            }
                                        }
                                    });
                                });
                                submit.trigger('click');
                            }
                        });
                        break;
                    case 'update':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else if (data.length > 1) {
                            layer.msg('只能同时编辑一个');
                        } else {
                            var categoryId = checkStatus.data[0].categoryId;
                            var url = '/restaurant/goodscategory/edit.html/'+categoryId;
                            layer.open({
                                type: 2
                                ,title: '编辑菜品类型'
                                ,content:url
                                ,maxmin: true
                                ,area: ['350px', '200px']
                                ,btn: ['确定', '取消']
                                ,yes: function(index, layero){
                                    var iframeWindow = window['layui-layer-iframe'+ index]
                                        ,submitID = 'LAY-user-front-submit'
                                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                    //监听提交
                                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                        var field = data.field; //获取提交的字段
                                        //提交 Ajax 成功后，静态更新表格中的数据
                                        $.ajax({
                                            url: "/restaurant/goodscategory/edit.do",
                                            type: "post",
                                            dataType: "JSON",
                                            data:field,
                                            success: function(result) {
                                                if(result.code == 200) {
                                                    table.reload('tableList'); //数据刷新
                                                    layer.close(index); //关闭弹层
                                                }else {
                                                    layer.alert(result.msg, {
                                                        title: '提交结果'
                                                        ,anim: 6
                                                    });
                                                }
                                            }
                                        });
                                    });
                                    submit.trigger('click');
                                }
                            });
                        }
                        break;
                    case 'delete':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else {
                            layer.confirm('确定删除?', function (){
                                var ids = '';
                                for (var i = 0; i < data.length; i++) {
                                    ids += data[i].categoryId + ",";
                                }
                                $.ajax({
                                    url: "/restaurant/goodscategory/del.do",
                                    type: "post",
                                    dataType: "JSON",
                                    data: {ids:ids},
                                    success: function(result) {
                                        if(result.code == 200) {
                                            table.reload('tableList'); //数据刷新
                                            layer.msg('删除成功!', {icon: 6});
                                        }else {
                                            layer.alert(result.msg, {
                                                title: '提交结果'
                                                ,anim: 6
                                            });
                                        }
                                    }
                                });
                            });

                        }
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(tableList)', function (obj) {
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'del') {
                    layer.confirm('确定删除?'+data.categoryName, function (index) {
                        $.ajax({
                            url: "/restaurant/goodscategory/del.do",
                            type: "post",
                            dataType: "JSON",
                            data: {ids:data.categoryId},
                            success: function(result) {
                                if(result.code == 200) {
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    layer.msg('删除成功!', {icon: 6});
                                }else {
                                    layer.alert(result.msg, {
                                        title: '提交结果'
                                        ,anim: 6
                                    });
                                }
                            }
                        });
                        layer.close(index);
                    });
                } else if (layEvent === 'edit') {
                    var url = '/restaurant/goodscategory/edit.html/'+data.categoryId;
                    layer.open({
                        type: 2
                        ,title: '编辑会员类型'
                        ,content:url
                        ,maxmin: true
                        ,area: ['350px', '200px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-user-front-submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: "/restaurant/goodscategory/edit.do",
                                    type: "post",
                                    dataType: "JSON",
                                    data:field,
                                    success: function(result) {
                                        if(result.code == 200) {
                                            table.reload('tableList'); //数据刷新
                                            layer.close(index); //关闭弹层
                                        }else {
                                            layer.alert(result.msg, {
                                                title: '提交结果'
                                                ,anim: 6
                                            });
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                }
            });
        });
    </script>
</body>
</html>